<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

if(isset($_SERVER['REMOTE_ADDR']) && !in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1'))) {
	die("HEY! I see what you did there. YOU'RE NOT LOCAL HOST! *points accusingly*");
}

require(dirname(dirname(__FILE__)). DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'ProjectConfiguration.class.php');

$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', true);
sfContext::createInstance($configuration);

$databaseManager = new sfDatabaseManager($configuration);

// get a cxn to the master
$conn = Propel::getConnection(NotePeer::DATABASE_NAME, Propel::CONNECTION_WRITE);

// join that identifies faulties
$sql = "SELECT notes.id FROM notes LEFT JOIN resource_version ON (resource_version.resource_name = 'Note' AND resource_version.resource_id = notes.id) WHERE (resource_version.resource_id IS NULL OR resource_version.resource_id='')";

// run it
$stmt = $conn->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll();

echo "Grabbed " . count($data) . " entries. Processing";

// for each one, call the model method
foreach($data as $row) {
	$id = array_shift($row);
	$note = NotePeer::retrieveByPk($id);
	$note->createVersionHistory($conn,$id);
	echo '.';
}

// ciao
echo "\n\nDone.\n\n";

